<?php
/**
 * Created by PhpStorm.
 * User: Eric
 * Email: 13522679763@163.com
 * Date: 2018/4/8
 * Time: 9:58
 */
namespace app\api\controller;

use think\Controller;
use think\Db;
use SMN\Client\DefaultSmnClient as DefaultSmnClient;
use GuzzleHttp\Psr7;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use app\api\model\UserIdentity;
/**
 *  验证码
 */
class Mobile extends Basic
{


    /**
     * 发送手机验证码
     */
    public function sendCaptcha()
    {
        $mobile = Input('post.mobile');
        $user = Input('post.user_id');
        $user_id = decode($user);
        if (!$user_id) {
            json_fail('获取用户id失败');
        }
        if (!$mobile) {
            json_fail('获取手机号失败!');
        }

        //判断手机号是否重复
        $user_identity_info = new UserIdentity();
        $check_mobile = $user_identity_info->where('mobile',$mobile)->where('status','neq',0)->find();
            if($check_mobile){
                json_fail('该手机号已注册');
            }

        // 初始化数据库模型
        $MobileCaptcha = Db('captcha');
        //取4位验证码
        $captcha = numberCaptcha();
        $str = '您的验证码:' . $captcha . ',10分钟有效,请安全保管';
        $res = $this->send_info($mobile, $str);
        //    判断是否发送短信成功
        if ($res == 1) {
            // 手机短信数据
            $sendData = array(
                'user_id' => $user_id,
                'mobile' => $mobile,
                'captcha' => $captcha,
                'create_time' => time(),
            );
            // 写入数据库
            $addResult = $MobileCaptcha->insertGetId($sendData);
            json_success($addResult);
        } else {
            json_fail('发送失败');
        }
    }

    /**
     * @param $mobile
     * @param $str
     * @return int  验证短信
     */
    public function send_info($mobile, $str)
    {
        $res = http_send_message($mobile,$str);
        if ($res['mata']['code'] == 0) {
            return 1;
        } else {
            return 2;
        }
    }

    /**
     * 验证手机
     */
    public function verificationMobile()
    {
        $user = Input('post.user_id');
        $user_id = decode($user);
        $phoneNum = Input('post.mobile');
        $captcha = Input('post.captcha');
        if (!$phoneNum) {
            json_fail('获取手机号码失败');
        }
        if (!$user_id) {
            json_fail('获取用户id失败');
        }
        if (!$captcha) {
            json_fail('验证码获取失败');
        }
        //判断验证码是否正确
        $info = $this->validCaptcha($phoneNum, $captcha, $user_id);
        if ($info == 0) {
            json_fail('验证码错误');
        } elseif ($info == -1) {
            json_fail('验证码失效');
        } else {
            //更改验证码使用状态
            Db('Captcha')->where(array('user_id' => $user_id, 'mobile' => $phoneNum, 'captcha' => $captcha))->update(array('status' => 1));
            // 返回用户
            json_success(1);
        }
    }

    /**
     * @param $mobile
     * @param $captcha
     * @param $user_id
     * @return int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 判断验证码是否正确
     */
    public function validCaptcha($mobile, $captcha, $user_id)
    {
        // 初始化数据库模型
        $MobileCaptcha = Db('captcha');
        // 查询条件
        $map = array(
            'mobile' => $mobile,
            'captcha' => $captcha,
            'status' => 2
        );
        // 判断用户id是否存在
        if ($user_id) {
            $map['user_id'] = $user_id;
        }
        // 查询是否存在未使用的验证码
        $info = $MobileCaptcha->where($map)->find();
        // 判断短信是否
        if ($info) {
            if ((time() - $info['create_time']) > 10 * 60) {
                // 验证码已过期
                return -1;
            } else {
                // 验证码成功
                return $info['id'];
            }
        } else {
            // 验证码不正确
            return 0;
        }
    }

}